package com.seele.tigerwallet.ui.activtiy;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.ui.MnemonicActivity;
import com.seele.tigerwallet.ui.base.BaseActivity;
import com.seele.tigerwallet.ui.manager.IScreenshotManager;
import com.seele.tigerwallet.ui.utils.BaseDialog;
import com.seele.tigerwallet.ui.views.NoScrollGridLayoutManager;
import com.seele.tigerwallet.utils.EthWalletUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建钱包
 */
public class CreateEthWalletActivity extends BaseActivity {

    protected static final String TAG = CreateEthWalletActivity.class.getSimpleName();

    private EthWalletAdapter mEthWalletAdapter;
    private RecyclerView mRecyclerView;
    private LinearLayout mLinearLayout;
    private Button mButton;
    private List<String> mnemonicCode = new ArrayList<>();//助记词
    private IScreenshotManager mIScreenshotManager;
    private BaseDialog mBaseDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_create_eth_wallet;
    }

    @Override
    protected void init() {
        initWidow();
        mRecyclerView = findViewById(R.id.recyclerView);
        mLinearLayout = findViewById(R.id.layout_mnemonic_refresh);
        mButton = findViewById(R.id.btn_record);
        NoScrollGridLayoutManager gridLayoutManager = new NoScrollGridLayoutManager(this, 3);
        gridLayoutManager.setScrollEnabled(false);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        if (null == mEthWalletAdapter) {
            mEthWalletAdapter = new EthWalletAdapter(R.layout.item_create_wallet);
        }
        mRecyclerView.setAdapter(mEthWalletAdapter);
        if (null == mIScreenshotManager) {
            mIScreenshotManager = IScreenshotManager.newInstance(this);
        }
        mIScreenshotManager.setListener(onScreenShotListener);
        mIScreenshotManager.startListen();

        getCode();
    }


    private void initDialog() {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.know_tv:
                        mBaseDialog.cancel();

                        break;
                }
            }
        };

        if (null == mBaseDialog) {
            mBaseDialog = new BaseDialog.Builder(this)
                    .view(R.layout.dialog_iscreenshot)
                    .setWidthPX((int) getResources().getDimension(R.dimen.dp_270))
                    .cancelTouchout(false)
                    .setDialogAnim(-2)
                    .setHeightPX((int) getResources().getDimension(R.dimen.dp_180))
                    .addViewOnclick(R.id.know_tv, listener)
                    .setDialogPosition(Gravity.CENTER) //设置dialog显示位置
                    .build();
        }

        mBaseDialog.show();


    }


    IScreenshotManager.OnScreenShotListener onScreenShotListener = new IScreenshotManager.OnScreenShotListener() {
        @Override
        public void onShot(String imagePath) {
            initDialog();
        }
    };

    /**
     * 设置控件的点击事件 可能造成数据混乱
     *
     * @param isEnabled
     */
    public void setEnabled(boolean isEnabled) {
        mLinearLayout.setEnabled(isEnabled);
        mButton.setEnabled(isEnabled);

    }

    /**
     * 获取
     */
    private void getCode() {
        setEnabled(false);
        getLoadingManager().showLoadingDialog();
        //获取助记词语
        new Thread(new Runnable() {
            @Override
            public void run() {
                mnemonicCode = EthWalletUtil.getMnemonicCode();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (null != mnemonicCode && mnemonicCode.size() > 0) {
                            mEthWalletAdapter.setNewData(mnemonicCode);
                            mEthWalletAdapter.notifyDataSetChanged();
                        }
                        setEnabled(true);
                        dismissLoadingDialog();
                    }
                });
            }
        }).start();


    }

    //什么是助记词的点击事件
    public void onTextClick(View v) {
        if (v.getId() == R.id.tv_mnemonic_exp) {
            startActivity(MnemonicActivity.class);
            return;
        }
    }

    //刷新助记词
    public void onLayoutClick(View view) {
        if (view.getId() == R.id.layout_mnemonic_refresh) {//刷新助记词
            getCode();
            return;
        }
    }

    //我记下来了
    public void onBtnClick(View view) {
        if (view.getId() == R.id.btn_record) {//确定已经记住助记词
            if (null != mnemonicCode && mnemonicCode.size() > 0) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList(ConfirmMnemonicActivity.TAG, (ArrayList<String>) mnemonicCode);
                startActivity(ConfirmMnemonicActivity.class, bundle);
                finish();
            }
        }
    }


    @Override
    protected void onDestroy() {
        mIScreenshotManager.stopListen();
        super.onDestroy();
    }
}